package com.lch.webdemo.common;


import com.lch.webdemo.common.msg.AjaxResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.URISyntaxException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author liuchenhui
 * @date 2018/10/22
 * @since
 */
@RestControllerAdvice
public class HttpExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public AjaxResult resolveException(HttpServletRequest request, Exception ex) {
        if (ex instanceof URISyntaxException || ex instanceof IllegalArgumentException) {
            LOGGER.warn("服务异常:", ex);
        } else {
            LOGGER.error("服务异常", ex);
        }
        return new AjaxResult(1, ex.getMessage(), false, null);
    }
}
